package pearson.automation.forms.courseDetails;

import java.util.HashMap;

import org.openqa.selenium.By;

import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;

import pearson.automation.forms.BaseFormWithSidebars;
import pearson.automation.forms.courseDetails.module.DropBoxForm;
import pearson.automation.forms.courseDetails.module.LearningObjectivesForm;
import pearson.automation.forms.courseDetails.module.ModuleForm;

/**
 * 
 * Form for a TOC part of a Course Form. Contains List of the modules.
 * 
 */
public class TOCForm extends BaseFormWithSidebars {

    private static Label identifyer =
        new Label(By.cssSelector("div[data-course-ref]"), "Course TOC");
    private Button dropBox =
        new Button(By.linkText("Drop Box"), "Drop Box");
    private Button learningObjectives =
        new Button(By.linkText("Learning Objectives"), "Learning Objectives");

    private Button newButton =
        new Button(By.id("course-toc-add-tools-button"), "New");
    private Button edit =
        new Button(By.id("course-toc-edit-tools-button"), "Edit");
    private Button newModule =
        new Button(By.className("js-add-module"),  "New Module");
    private Button newSection =
        new Button(By.className("js-add-section"), "New Section");
    private Button done =
        new Button(By.xpath("//section[@id='edit-nav']//a[@title='Done']"),
                "Done");
    
    private ElementList moduleList =
        new ElementList(By.xpath("//li[@data-module-type]"),
                "Module list");
    
    private HashMap<String, ModuleForm> modules;

    public TOCForm(By titleLocator, String title) {
        super(titleLocator, title);
    }

    public TOCForm(String[] moduleNames) {
        super(identifyer.getLocator(), identifyer.getName());
        modules = new HashMap<String, ModuleForm>();
        for (String name : moduleNames) {
            modules.put(name, ModuleForm.init(name));
        }
    }

    public ModuleForm getModule(String moduleName) {
        if (modules.get(moduleName) == null) {
            ModuleForm newModule = ModuleForm.init(moduleName);
            modules.put(moduleName, newModule);
        }
        return modules.get(moduleName);
    }

    public DropBoxForm openDropBox() {
        dropBox.clickAndWait();
        return new DropBoxForm();
    }

    public LearningObjectivesForm openLearningObjectives() {
        learningObjectives.clickAndWait();
        return new LearningObjectivesForm();
    }
    
    public void clickNewButton() {
        newButton.clickAndWait();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public void clickEditButton() {
        edit.clickAndWait();
    }
    
    public void clickDoneButton() {
        done.clickAndWait();
    }
    
    public boolean isNewModuleButtonEnabled() {
        return newModule.isEnabled();
    }
    
    public void clickNewModuleButton() {
        newModule.clickAndWait();
    }
    
    public boolean isNewSectionButtonEnabled() {
        return newSection.isEnabled();
    }
    
    public void clickNewSectionButton() {
        newSection.clickAndWait();
    }
    
    public int getModuleSize(){
        return moduleList.getListElements().size();
    }
}
